package com.springboot01.controller;

import com.springboot01.entity.User;
import com.springboot01.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author MagicMushroom
 * @date 2021/6/29
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> all = userService.findAll();
        model.addAttribute("userList",all);
//        for (User user : all) {
//            log.debug(user.toString());
//        }
        return "userList";
    }

    @RequestMapping("/addOne")
    public String addOne(User user){
        userService.insertOne(user);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/findOne")
    public String findOne(Integer id,Model model){
        User user = userService.findOne(id);
        model.addAttribute("user",user);
        return "showOne";
    }

    @RequestMapping("/update")
    public String update(User user){
        userService.update(user);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/deleteOne")
    public String deleteOne(Integer id){
        userService.deleteOne(id);
        return "redirect:/user/findAll";
    }

}
